{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GeoViews Tests\n",
    "\n",
    "<img align=\"right\" src=\"https://anitagraser.github.io/movingpandas/assets/img/movingpandas.png\">\n",
    "\n",
    "This notebook contains simple test cases for **GeoViews** plotting behavior."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib\n",
    "import os\n",
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "from geopandas import GeoDataFrame, read_file\n",
    "from shapely.geometry import Point, LineString, Polygon\n",
    "from fiona.crs import from_epsg\n",
    "from datetime import datetime, timedelta\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "import geoviews\n",
    "import bokeh\n",
    "import hvplot\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"..\")\n",
    "import movingpandas as mpd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(mpd.__version__)\n",
    "print(geoviews.__version__)\n",
    "print(bokeh.__version__)\n",
    "print(hvplot.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point_df = GeoDataFrame(pd.DataFrame([\n",
    "  {'geometry':Point(0,0), 'a':1},\n",
    "  {'geometry':Point(6,0), 'a':2},\n",
    "  {'geometry':Point(6,6), 'a':3},\n",
    "  {'geometry':Point(9,9), 'a':4}\n",
    "]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point_df.hvplot(geo=True, hover_cols=['a'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "point_df.hvplot(geo=True, c='x', hover_cols=['a'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "line_df = GeoDataFrame(pd.DataFrame([\n",
    "  {'geometry':LineString([(0,0), (1,1)]), 'a':1},\n",
    "  {'geometry':LineString([(2,2), (3,4)]), 'a':2}\n",
    "]))\n",
    "\n",
    "line_df.hvplot(geo=True, hover_cols=['a'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "line_df.hvplot(geo=True, c='x', hover_cols=['a'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "line_df.plot(column='a')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Work-around using dim() to fix Bokeh rendering issues (https://github.com/anitagraser/movingpandas/issues/71):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from holoviews import dim\n",
    "line_df.hvplot(geo=True, c=dim('a'), hover_cols=['a'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3D (holoviews)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import holoviews as hv\n",
    "print(hv.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pt3d = GeoDataFrame(pd.DataFrame([\n",
    "  {'geometry':Point(0,0,0), 't':datetime(2018,1,1,12,0,0)},\n",
    "  {'geometry':Point(6,0,0.1), 't':datetime(2018,1,1,12,6,0)},\n",
    "  {'geometry':Point(6,6,3), 't':datetime(2018,1,1,12,10,0)},\n",
    "  {'geometry':Point(9,9,2), 't':datetime(2018,1,1,12,15,0)}\n",
    "]), crs=31256).set_index('t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pt3d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "http://holoviews.org/reference/elements/matplotlib/Path3D.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.extension('matplotlib')\n",
    "path = hv.Path3D((pt3d.geometry.x, pt3d.geometry.y, pt3d.geometry.z))\n",
    "path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "traj3d = mpd.Trajectory(pt3d, 1)\n",
    "traj3d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.extension('matplotlib')\n",
    "scatter = hv.Scatter3D((pt3d.geometry.x, pt3d.geometry.y, pt3d.geometry.z))  \n",
    "scatter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.extension('bokeh')\n",
    "pt3d.hvplot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3D path docs\n",
    "\n",
    "http://holoviews.org/reference/elements/matplotlib/Path3D.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "line = np.random.randn(500, 3).cumsum(axis=0)\n",
    "path = hv.Path3D(line)\n",
    "\n",
    "path.opts(azimuth=40, elevation=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "paths = [{('x', 'y', 'z'): np.random.randn(500, 3).cumsum(axis=0), 'index': i} for i in range(3)]\n",
    "hv.Path3D(paths, vdims='index').opts(color='index', cmap=['red', 'green', 'blue'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "scatter = hv.Scatter3D({('x', 'y', 'z'): line, 'index': np.arange(500)}, vdims='index')\n",
    "\n",
    "path * scatter.opts(color='index', cmap='viridis', s=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
